# -*- coding: utf-8 -*-
# !/usr/bin/env python
from utils.mysql_utils import query_to_list


def query_env_hosts(env_id=''):
    sql_parameter = []

    if env_id:
        sql_fragment = "WHERE a.env_id = %s"
        sql_parameter.append(env_id)
    else:
        sql_fragment = ''

    sql = """
    SELECT *
    FROM hosts_management a
    %s
    AND status = 1
    """%sql_fragment
    data = query_to_list(sql, sql_parameter, False)

    return data


def query_env_hosts_k8s(env_id=''):
    sql_parameter = []

    if env_id:
        sql_fragment = "WHERE a.env_id = %s"
        sql_parameter.append(env_id)
    else:
        sql_fragment = ''

    sql = """
    SELECT a.*, b.kubernetes_manage_id, b.kubernetes_cluster_id, b.kubernetes_role, b.deploy_event_ids, 
    c.sys_code_name AS k8s_roles_type_text
    FROM hosts_management a
    LEFT JOIN kubernetes_manage b ON a.host_id = b.host_id
    LEFT JOIN sys_code c ON c.sys_code = b.kubernetes_role AND c.sys_code_type = "k8s_roles_type"
    %s
    AND a.status = 1
    """%sql_fragment
    data = query_to_list(sql, sql_parameter, False)

    return data


def query_env_hosts_soft(env_id=''):
    sql_parameter = []

    if env_id:
        sql_fragment = "WHERE a.env_id = %s"
        sql_parameter.append(env_id)
    else:
        sql_fragment = ''

    sql = """
    SELECT a.*,b.soft_nick,b.soft_code,b.soft_install_on,b.soft_log_path,b.soft_install_code,b.soft_install_url,b.soft_id,
    b.soft_install_cmd,b.soft_start_cmd,b.soft_restart_cmd,b.soft_stop_cmd,b.comment as soft_comment, b.package_id, 
    c.sys_code_name as soft_code_text, d.sys_code_name as soft_install_code_text
    FROM hosts_management a
    LEFT JOIN soft_management b ON a.host_id = b.host_id AND b.status = 1
    LEFT JOIN sys_code c on c.sys_code = b.soft_code and c.sys_code_type = 'soft_type'
    LEFT JOIN sys_code d on d.sys_code = b.soft_install_code and d.sys_code_type = 'soft_install_type'
    %s
    AND a.status = 1
    """%sql_fragment
    data = query_to_list(sql, sql_parameter, False)

    return data